<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轻量级HTTP服务器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
        }
        h2 {
            color: #555;
            margin-top: 30px;
        }
        pre {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
        code {
            font-family: Consolas, Monaco, 'Andale Mono', monospace;
        }
        .feature {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }
        .url {
            color: #0066cc;
            font-family: monospace;
        }
        button {
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        #response {
            margin-top: 20px;
            padding: 15px;
            background-color: #f0f0f0;
            border-radius: 5px;
            min-height: 50px;
        }
    </style>
</head>
<body>
    <h1>轻量级HTTP服务器</h1>
    <p>这是一个用C语言实现的轻量级HTTP服务器，支持以下功能：</p>

    <div class="feature">
        <h2>查看版本号</h2>
        <p>通过访问以下URL获取服务器版本信息：</p>
        <pre><code class="url">http://localhost:8080/version</code></pre>
        <button onclick="fetchData('/version')">获取版本号</button>
    </div>

    <div class="feature">
        <h2>列出文件列表</h2>
        <p>通过访问以下URL获取服务器当前目录下的文件列表：</p>
        <pre><code class="url">http://localhost:8080/files</code></pre>
        <button onclick="fetchData('/files')">获取文件列表</button>
    </div>

    <div class="feature">
        <h2>查看服务器配置</h2>
        <p>通过访问以下URL获取服务器配置信息：</p>
        <pre><code class="url">http://localhost:8080/config</code></pre>
        <button onclick="fetchData('/config')">获取配置信息</button>
    </div>

    <div class="feature">
        <h2>CGI功能</h2>
        <p>服务器支持以下CGI功能：</p>
        <ul>
            <li> ping - 测试服务器响应：<code class="url">http://localhost:8080/cgi/ping</code></li>
            <li> echo - 回显参数：<code class="url">http://localhost:8080/cgi/echo?param1=value1&param2=value2</code></li>
            <li> get_time - 获取当前时间：<code class="url">http://localhost:8080/cgi/get_time</code></li>
        </ul>
        <button onclick="fetchData('/cgi/ping')">测试Ping</button>
        <button onclick="fetchData('/cgi/echo?name=test&message=hello')">测试Echo</button>
        <button onclick="fetchData('/cgi/get_time')">获取当前时间</button>
    </div>

    <div class="feature">
        <h2>响应结果</h2>
        <div id="response"></div>
    </div>

    <script>
        function fetchData(url) {
            fetch(url)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('response').innerText = JSON.stringify(data, null, 2);
                })
                .catch(error => {
                    document.getElementById('response').innerText = '请求错误: ' + error;
                });
        }
    </script>
</body>
</html>